﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Forms;
using MINERP.BLL.ProductConditionFile;
using MINERP.COM;
using Timer = System.Timers.Timer;

namespace MINERP.ProductConfitionFile
{
    public partial class FrmProductCondition : Form
    {
        public FrmProductCondition()
        {
            InitializeComponent();
        }
        ProductConditionFileBLL _bll = new ProductConditionFileBLL();
        private void FrmProductCondition_Load(object sender, EventArgs e)
        {
            labelShowMessage.Text = "正在获取要生成Condition的产品...";
            labelCount.Text = "累计生成condition文件：0";
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            _bll.ShowCurrPointHandle += new ProductConditionFileBLL.ShowProcess(SetProcessPoint);
            _bll.ShowMsgHandle += new ProductConditionFileBLL.ShowMessage(DisplayMessage);
            _bll.ShowLastResultHandle += new ProductConditionFileBLL.ShowResult(ShowReaultViws);
            _bll.Techid = 16;
            StartProduct();
        }
        private delegate void DelegateSetResultList(OrderFileView view);



        void ShowReaultViws(OrderFileView orderFileView)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new DelegateSetResultList(ShowReaultViws), new object[] { orderFileView });
            }
            else
            {
                OrderFileView oldView = DataSurceList.FirstOrDefault(p => p.FolderName == orderFileView.FolderName);
                if (oldView != null)
                {
                    DataSurceList.Remove(oldView);
                }
                DataSurceList.Add(orderFileView);
                gridControlOederFile.DataSource = DataSurceList.OrderByDescending(p => p.CreateTime).ToList();
                gridControlOederFile.RefreshDataSource();
                labelCount.Text = "累计生成condition文件：" + DataSurceList.Count.ToString();
            }
        }
        private static bool CompareView(OrderFileView o1, OrderFileView o2)
        {

            if (o1 != null && o2 != null && (o1.OrderFileID == o2.OrderFileID))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private delegate void DelegateSetLeableValue(string str);
        void DisplayMessage(string str)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new DelegateSetLeableValue(DisplayMessage), new object[] { str });
            }
            else
            {
                labelShowMessage.Text = str;
            }
        }
        private delegate void DelegateSetProcessValue(int value);

        void SetProcessPoint(int inValue)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new DelegateSetProcessValue(SetProcessPoint), new object[] { inValue });
            }
            else
            {
                progressBar1.Value = inValue;

            }
        }


        private void FrmProductCondition_FormClosing(object sender, FormClosingEventArgs e)
        {
            EndProduct();
            this.Visible = false;
            e.Cancel = true;
        }
        Timer timer = new Timer();
        private bool EnableProduct = false;

        private List<OrderFileView> DataSurceList = new List<OrderFileView>();
        public void StartProduct()
        {
            //_bll.AutoPruductConditionFile();
            timer.Interval = 30000;//30秒
            timer.Enabled = true;
            timer_Elapsed(null, null);
        }
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                timer.Enabled = false;
                
               int intVal= _bll.AutoPruductConditionFile();
               if (intVal == 0)
               {
                   timer.Interval = 100;
               }
               else
               {
                   timer.Interval = 30000;
               }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "数据库连接错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {

                timer.Enabled = true;

            }


        }
        public void EndProduct()
        {
            EnableProduct = false;

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
        }

        private void FrmProductCondition_Deactivate(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                //this.Width = 704;
                //this.Height = 85;
                //Rectangle rect = Screen.PrimaryScreen.WorkingArea;
                //this.Location = new Point(rect.Width - this.Width, rect.Height - this.Height-20);
            }
            //else if(this.WindowState == FormWindowState.Maximized||this.WindowState == FormWindowState.Normal)
            //{
            //    this.ShowInTaskbar = true;
            //    this.Width = 704;
            //    this.Height = 460;
            //    Rectangle rect = Screen.PrimaryScreen.WorkingArea;
            //    this.Location = new Point(rect.Width/2-this.Width/2,rect.Height/2-this.Width/2);
            //}


        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.Visible = true;
            this.Width = 704;
            this.Height = 460;
            Rectangle rect = Screen.PrimaryScreen.WorkingArea;
            this.Location = new Point(rect.Width / 2 - this.Width / 2, rect.Height / 2 - this.Width / 2);
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.Visible = false;
        }

        private void gridViewOederFile_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >=0)
            {
                OrderFileView view = (OrderFileView)gridViewOederFile.GetRow(e.RowHandle);
                if (view.IsError == 1)
                {
                    e.Appearance.BackColor = Color.Red;
                }
                else
                {
                    e.Appearance.BackColor = Color.Green;
                }
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            Environment.Exit(0);
        }

    }
   
}
